import { Define, EntityObjectFactory } from 'tiny-entity2';
const EntityObjectType = EntityObjectFactory.GetEntityObjectType(process.env.dataBaseType || 'mysql');
import { PromotionItem } from './promotionItem';

@Define.Table({ TableName: "promotion" })
export class Promotion extends EntityObjectType<Promotion>{
    @Define.PrimaryKey()
    id: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    name: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true
    })
    type: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    description: string;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    beginDate: number;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    endDate: number;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    beginTime: number;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    endTime: number;

    @Define.Column({
        DataType: Define.DataType.Array
    })
    loopWeekOfDays: number[];

    @Define.Column({
        DataType: Define.DataType.Array
    })
    loopMonthOfDays: number[];

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    takeEffectWay: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true
    })
    mode: string;

    @Define.Column({
        DataType: Define.DataType.Decimal,
        DecimalPoint: 2,
        DataLength: 10,
        DefaultValue: 0
    })
    value: number;

    /**
     * 一个JSON字符串存储适用于此活动的用户
     *
     * @type {string}
     * @memberof Promotion
     */
    @Define.Column({
        DataType: Define.DataType.JSON
    })
    includeMembers: IncludeMember;

    /**
     * 每【every】件商品 【each】件商品参与折扣
     *
     * @type {number}
     * @memberof Promotion
     */
    @Define.Column({
        DataType: Define.DataType.Decimal
    })
    each: number;

    /**
     * 每【every】件商品 【each】件商品参与折扣
     *
     * @type {number}
     * @memberof Promotion
     */
    @Define.Column({
        DataType: Define.DataType.Decimal
    })
    every: number;

    @Define.Column({
        DataType: Define.DataType.Decimal
    })
    limtAmount: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        IsIndex: true,
        NotAllowNULL: true
    })
    storeId: string;

    /**
     * 权重 
     * 用于结算的时候活动计算的先后顺序
     * @type {number}
     * @memberof Promotion
     */
    @Define.Column({
        DataType: Define.DataType.INT,
        DefaultValue: 0
    })
    weight: number;

    /**
     *  显示排序
     *
     * @type {number}
     * @memberof Promotion
     */
    @Define.Column({
        DataType: Define.DataType.INT,
        DefaultValue: 0
    })
    index: number;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    createTime: number;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    version: number;

    @Define.Mapping({
        Mapping: 'PromotionItem',
        MappingKey: 'promotionId'
    })
    promotionItems: PromotionItem[];
}

export const PromotionType = {
    Order: 'order',         //整单活动
    Product: 'product'      //单项活动
}

export const TakeEffectWay = {
    Manual: 'manual',       //手动应用
    Auto: 'auto'           //自动应用
}

export const PromotionMode = {
    DiscountMode: 'discountMode',   //折扣活动
    CashMode: 'cashMode',            //现金活动
    MemberPrice: 'memberPrice'      // 会员价
}

interface IncludeMember {
    mode: 'all' | 'someone' | 'memberType';
    userIds?: string[];
    memberTypeIds?: string[];
}

